package com.leetroll.app.annotation.OperationLogger;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.leetroll.api.global.BaseRedisConf;
import com.leetroll.api.global.Constants;
import com.leetroll.api.global.SysConf;
import com.leetroll.common.entity.SysLog;
import com.leetroll.common.holder.AbstractRequestAwareRunnable;
import com.leetroll.common.utils.IpUtils;
import com.leetroll.common.utils.RedisUtil;
import com.leetroll.common.utils.StringUtils;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 异步记录日志
 */
public class SysLogHandle extends AbstractRequestAwareRunnable {

    /**
     * Redis工具列
     */
    RedisUtil redisUtil;

    /**
     * 参数列表
     */
    private final String paramsJson;

    /**
     * 类路径
     */
    private final String classPath;

    /**
     * 方法名
     */
    private final String methodName;

    /**
     * 方法请求时间
     */
    private final Date startTime;

    /**
     * 操作名称
     */
    private final String operationName;

    /**
     * 请求IP
     */
    private final String ip;

    /**
     * 请求类型
     */
    private final String type;

    /**
     * 请求URL
     */
    private final String requestUrl;

    private final String userId;
    private final String userName;

    /**
     * 构造函数
     */
    public SysLogHandle(String ip, String type, String requestUrl, String userId,String userName,
                        String paramsJson, String classPath,
                        String methodName, String operationName,
                        Date startTime, RedisUtil redisUtil) {
        this.ip = ip;
        this.type = type;
        this.requestUrl = requestUrl;
        this.userId = userId;
        this.userName = userName;
        this.paramsJson = paramsJson;
        this.classPath = classPath;
        this.methodName = methodName;
        this.operationName = operationName;
        this.startTime = startTime;
        this.redisUtil = redisUtil;
    }

    @Override
    protected void onRun() {
        SysLog sysLog = new SysLog();
        //从Redis中获取IP来源
        String jsonResult = redisUtil.get(BaseRedisConf.IP_SOURCE + Constants.SYMBOL_COLON + ip);
        if (StringUtils.isEmpty(jsonResult)) {
            String addresses = IpUtils.getAddresses(SysConf.IP + SysConf.EQUAL_TO + ip, SysConf.UTF_8);
            if (StringUtils.isNotEmpty(addresses)) {
                sysLog.setIpSource(addresses);
                redisUtil.setEx(BaseRedisConf.IP_SOURCE + Constants.SYMBOL_COLON + ip, addresses, 24, TimeUnit.HOURS);
            }
        } else {
            sysLog.setIpSource(jsonResult);
        }

        //设置请求信息
        sysLog.setIp(ip);
        //设置调用的类
        sysLog.setClassPath(classPath);
        //设置调用的方法
        sysLog.setMethod(methodName);
        //设置Request的请求方式 GET POST
        sysLog.setType(type);
        sysLog.setUrl(requestUrl);
        sysLog.setOperation(operationName);
        sysLog.setCreateTime(new Date());
        sysLog.setUpdateTime(new Date());
        sysLog.setUserId(userId);
        sysLog.setUserName(userName);
        sysLog.setParams(paramsJson);
        Date endTime = new Date();
//        Long spendTime = DateUtil.between(startTime, endTime, DateUnit.MS);
//        // 计算请求接口花费的时间，单位毫秒
//        sysLog.setSpendTime(spendTime);
        sysLog.insert();
    }
}
